<?php

/*

Smart Bot (smart_bot2.php)
 
*/

$page_title = "Smart Bot";
$rank_check = 0;
include "header.inc.php";

if ($page == "add2")
{
	if ($rank < 7) { die("$openHTML<p>You must be an admin to view this page.$closeHTML"); }

	mysql_query("INSERT INTO smart_bot (question,answer,keyword,search_action) VALUES ('$question','$answer','$keyword','$search_action')");
	header("Location: smart_bot2.php?page=add&game=$game");
}

print "$openHTML";

if (!$page) {

ECHO <<<END

	<form action=smart_bot2.php?page=search&game=$game method=post>
	<p align=center>
	Hey Smart Bot, I was just wondering:<Br>
	<input type=text name=question size=60 value="Type your question here"><br>
	<input type=submit value="Ask Away">
	</p>
	</form>

END;

}

if ($page == "search")
{
	$c = 0;
	$question = str_replace("?", "", $question);
	$countQ = count(explode(" ", $question));
	$question2 = explode(" ", $question);

	$findQ = mysql_query("SELECT * FROM smart_bot WHERE question LIKE '%$question2[0]%'");
	while ($getQ = mysql_fetch_array($findQ))
	{
		for ($x = 1; $x <= $countQ;)
		{
			$findQ2 = mysql_query("SELECT * FROM smart_bot WHERE question LIKE '%$question2[0]%' AND  question LIKE '%$question2[$x]%'");
			while ($getQ2 = mysql_fetch_array($findQ2))
			{
				$id = $getQ2[id];
				$rel[$id] += 1;
				$arrayQID .= "$id, ";
				$findQ3 = mysql_query("SELECT * FROM smart_bot WHERE keyword LIKE '%$question2[$x]%' AND  question LIKE '%$question2[0]%' AND  question LIKE '%$question2[$x]%'");
				while ($getQ3 = mysql_fetch_array($findQ3))
				{
					$id = $getQ2[id];
					$rel[$id] += 3;
				}
			}
			$x++;
		}
	}

	$arrayQs = array($arrayQID);
	$countQs = count($arrayQs);
	for ($q = 1; $q <= $countQs;)
	{
		$id = $arrayQs[$q];
		if ($rel[$id] >= 4)
		{
			$findQ4 = fetch("SELECT * FROM smart_bot WHERE id = '$id'");
			print "<a href=smart_bot2.php?page=view&id=$id&game=$game>$findQ4[question]</a> (Relevancy: $rel[$id])<Br>";
		}
		$c++;
		$q++;
	}

	if ($c == 0) { print "<p><i>Sorry $username, I couldn't find ANY questions with your search terms. Try again with some different words.</i></p><p>Search Tips:<br><br>* Be sure to use full words, no abbreviations. <br>* Make sure all words are spelled correctly.<Br>* Try different keywords.<br>* Try more general keywords."; }
}


if ($page == "view")
{
	$findQ = fetch("SELECT * FROM smart_bot WHERE id = '$id'");

	$findPage[page_info] = "$findQ[answer]";
	include "showpage.inc.php";

	print "

	<p><b>$findQ[question]</b></p>

	<p>$findPage[page_info]</p>

	<p>Related Questions:</p>

	";

	$findQs = mysql_query("SELECT * FROM smart_bot WHERE keyword = '$findQ[keyword]' AND id != '$id'");
	while ($getQs = mysql_fetch_array($findQs))
	{
		print "<font size=-1><a href=smart_bot2.php?page=view&id=$getQs[id]&game=$game>$getQs[question]</a></font><Br>";
		$d++;
	}

	if ($d <= 0) { print "<i>No related questions</i>"; }

	print "</p>";
}

if ($page == "add")
{
if ($rank < 7) { die("You must be an admin to view this page.$closeHTML"); }
ECHO <<<END

<p>
<form action=smart_bot2.php?page=add2&game=$game method=post>
Question:<br>
<input type=text name=question size=60><br><br>

Answer:<br>
<textarea name=answer cols=52 rows=10 name=answer></textarea><br><br>

Keyword:<br>
<input type=text name=keyword> (pets, items, battle, mail, etc.)<br><br>

Keyword 2:<br>
<input type=text name=search_action> (what, where, how, is, do etc.)<br><br>

<input type=submit value="Add Entry">
</form>
</p>
END;
}

print "$closeHTML";

?>